﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProDivingAdmin2salmon14
{
    public partial class AddDataForm : Form, IAddDataForm
    {
        private bool isEdit = false;

        public AddDataForm()
        {
            InitializeComponent();
            this.DiverGridView.Columns.Add("DiverName", "Name");
            this.DiverGridView.Columns.Add("DiverPersonNo", "Parson No: (YYMMDDXXXX)");
            this.DiverGridView.Columns.Add("DiverNationality", "Nationality");
            this.DiverGridView.Columns.Add("DiverID", "Diver ID");
            this.DiverGridView.RowHeadersVisible = false;
            this.DiverGridView.Columns["DiverName"].ReadOnly = true;
            this.DiverGridView.Columns["DiverPersonNo"].ReadOnly = true;
            this.DiverGridView.Columns["DiverNationality"].ReadOnly = true;
            this.DiverGridView.Columns["DiverID"].ReadOnly = true;
            this.DiverGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
            this.DiverGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.DiverGridView.CellEndEdit += new DataGridViewCellEventHandler(OnDiverCellEnterd);
            this.DiverGridView.AllowUserToAddRows = false;
            this.DiverGridView.CellMouseClick += new DataGridViewCellMouseEventHandler(OnCheckDiver);

            this.DiveGridView.Columns.Add("DiveCode", "Dive code");
            this.DiveGridView.Columns.Add("DiveName", "Dive name");
            this.DiveGridView.RowHeadersVisible = false;
            this.DiveGridView.Columns["DiveCode"].ReadOnly = true;
            this.DiveGridView.Columns["DiveName"].ReadOnly = true;
            this.DiveGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            this.DiveGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
            this.DiveGridView.CellEndEdit += new DataGridViewCellEventHandler(OnDiveCodeEntered);
            this.DiveGridView.AllowUserToAddRows = false;
        }

        private void OnCheckDiver(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (EventGetDiveListFromDiver != null) 
            {
                this.DiveGridView.Rows.Clear();
                foreach(var dive in EventGetDiveListFromDiver(new Diver("","", Int32.Parse(this.DiverGridView.CurrentRow.Cells["DiverID"].Value.ToString()),"",new List<Dive>())))
                {
                    this.DiveGridView.Rows.Add(new DataGridViewRow());
                    this.DiveGridView.Rows[this.DiveGridView.Rows.Count - 1].Cells["DiveCode"].Value = dive.diveCode;
                    this.DiveGridView.Rows[this.DiveGridView.Rows.Count - 1].Cells["DiveName"].Value = EventGetDiveName(dive.diveCode);
                }
            }
        }

        private void OnDiverCellEnterd(object sender, DataGridViewCellEventArgs e)
        {
            this.DiverGridView.CurrentRow.Cells[this.DiverGridView.CurrentCell.ColumnIndex].ReadOnly = true;
            if (this.DiverGridView.CurrentCell.ColumnIndex + 1 <= 2)
            {
                if (this.DiverGridView.CurrentCell.ColumnIndex == 1)
                {
                    if (Regex.Matches(this.DiverGridView.CurrentCell.Value.ToString(), @"[a-zA-Z]").Count > 0)
                    {
                        this.DiverGridView.CurrentRow.Cells[this.DiverGridView.CurrentCell.ColumnIndex].ReadOnly = false;
                        this.DiverGridView.CurrentCell.Selected = true;
                        this.DiverGridView.BeginEdit(true);
                        return;
                    }
                    else if (this.DiverGridView.CurrentCell.Value.ToString().Length > 10 || this.DiverGridView.CurrentCell.Value.ToString().Length < 10)
                    {
                        this.DiverGridView.CurrentCell.Selected = true;
                        this.DiverGridView.BeginEdit(true);
                        return;
                    }
                }
                this.DiverGridView.CurrentRow.Cells[this.DiverGridView.CurrentCell.ColumnIndex + 1].ReadOnly = false;
                this.DiverGridView.CurrentRow.Cells[this.DiverGridView.CurrentCell.ColumnIndex + 1].Selected = true;
                this.DiverGridView.BeginEdit(true);
            }
            else 
            {
                if (EventAddDiverToList != null) 
                {
                    this.DiverGridView.CurrentRow.Cells["DiverID"].Value = this.DiverGridView.CurrentRow.Index;
                    EventAddDiverToList(new Diver(this.DiverGridView.CurrentRow.Cells["DiverName"].Value.ToString(),this.DiverGridView.CurrentRow.Cells["DiverPersonNo"].Value.ToString(), Int32.Parse(this.DiverGridView.CurrentRow.Cells["DiverID"].Value.ToString()), this.DiverGridView.CurrentRow.Cells["DiverNationality"].Value.ToString(), new List<Dive>()));
                }
            }
        }

        private void OnDiveCodeEntered(object sender, DataGridViewCellEventArgs e)
        {
            if (this.isEdit)
            {
                if (EventGetDiveName != null)
                {
                    this.DiveGridView.CurrentRow.Cells["DiveName"].Value = EventGetDiveName(this.DiveGridView.CurrentCell.Value.ToString().ToUpper());
                }

                this.isEdit = false;
                this.DiveGridView.CurrentRow.Cells["DiveCode"].Value = this.DiveGridView.CurrentRow.Cells["DiveCode"].Value.ToString().ToUpper();
                this.DiveGridView.CurrentCell.ReadOnly = true;

                if (EventAddDiveToDiver != null) 
                {
                    EventAddDiveToDiver(new Diver("","", Int32.Parse(this.DiverGridView.CurrentRow.Cells["DiverID"].Value.ToString()),"",new List<Dive>()), new Dive(this.DiveGridView.CurrentRow.Cells["DiveCode"].Value.ToString()));
                }
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog diverListFilePath = new OpenFileDialog();

            diverListFilePath.Filter = "Text Files (.txt)|*.txt";
            diverListFilePath.FilterIndex = 0;

            diverListFilePath.Multiselect = false;
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string toFewDives = "";
            string toManyDives = "";

            if (EventGetNewDiverList != null)
            {
                foreach (var diver in EventGetNewDiverList())
                {
                    if (diver.diveList.Count < 5)
                    {
                        toFewDives = toFewDives.Insert(toFewDives.Length, ", " + diver.fullName);
                    }
                    else if (diver.diveList.Count > 5)
                    {
                        toManyDives = toManyDives.Insert(toManyDives.Length, ", " + diver.fullName);
                    }
                }
            }

            if (toFewDives == "" && toManyDives == "")
            {
                SaveFileDialog filePath = new SaveFileDialog();

                filePath.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";

                DialogResult result = filePath.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    if (EventGetNewDiverList != null && EventSaveDivers != null)
                    {
                        EventSaveDivers(EventGetNewDiverList(), filePath.FileName);
                    }
                }
            }
            else 
            {
                MessageBox.Show(this, "To few dives on:" + Environment.NewLine + toFewDives + Environment.NewLine + Environment.NewLine + "To many dives on:" + Environment.NewLine + toManyDives, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void AddDiver_Click(object sender, EventArgs e)
        {
            this.DiverGridView.Rows.Add(new DataGridViewRow());
            this.DiverGridView.Rows[this.DiverGridView.Rows.Count - 1].Cells["DiverName"].Selected = true;
            this.DiverGridView.Rows[this.DiverGridView.Rows.Count - 1].Cells["DiverName"].ReadOnly = false;
            this.DiverGridView.BeginEdit(true);
        }

        private void DeleteDiver_Click(object sender, EventArgs e)
        {
            try
            {
                this.DiverGridView.Rows.Remove(this.DiverGridView.CurrentRow);
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Can't delete something that doesn't exist!");
            }
        }

        private void EditDiver_Click(object sender, EventArgs e)
        {
            try
            {
                this.DiverGridView.CurrentCell.ReadOnly = false;
                this.DiverGridView.BeginEdit(true);
            }
            catch (NullReferenceException) 
            {
                System.Console.WriteLine("Can't edit something that doesn't exist!");
            }
        }

        private void AddDive_Click(object sender, EventArgs e)
        {
            this.DiveGridView.Rows.Add(new DataGridViewRow());
            this.DiveGridView.Rows[this.DiveGridView.Rows.Count - 1].Cells["DiveCode"].Selected = true;
            this.DiveGridView.Rows[this.DiveGridView.Rows.Count - 1].Cells["DiveCode"].ReadOnly = false;
            this.DiveGridView.BeginEdit(true);
            this.isEdit = true;
        }

        private void DeleteDive_Click(object sender, EventArgs e)
        {
            try
            {
                if (EventDeleteDiveFromDiver != null)
                {
                    EventDeleteDiveFromDiver(new Diver("", "", Int32.Parse(this.DiverGridView.CurrentRow.Cells["DiverID"].Value.ToString()), "", new List<Dive>()), this.DiveGridView.CurrentRow.Index);
                    this.DiveGridView.Rows.Remove(this.DiveGridView.CurrentRow);
                }
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Can't delete something that doesn't exist!");
            }
        }

        private void EditDive_Click(object sender, EventArgs e)
        {
            try
            {
                this.DiveGridView.CurrentRow.Cells["DiveCode"].ReadOnly = false;
                this.DiveGridView.BeginEdit(true);
                this.isEdit = true;
            }
            catch (NullReferenceException) 
            {
                System.Console.WriteLine("Can't edit something that doesn't exist!");
            }
        }

        public event DelegateAddDiverToList EventAddDiverToList = null;

        public event DelegateAddDiveToDiver EventAddDiveToDiver = null;

        public event DelegateGetDiveListFromDiver EventGetDiveListFromDiver = null;

        public event DelegateGetDiveName EventGetDiveName = null;

        public event DelegateDeleteDiveFromDiver EventDeleteDiveFromDiver = null;

        public event DelegateGetNewDiverList EventGetNewDiverList = null;

        public event DelegateSaveDivers EventSaveDivers = null;

    }
}
